package net.sending;

import net.message.Message;
import net.Client;
import net.receiving.converter.Converter;

import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;

public class Unicast implements UnicastMessage {

    private final Message message;
    private final Client client;

    public Unicast(Message message, Client client) {
        this.client = client;
        this.message = message;
    }

    public void send() {
        try {
            Socket socket = SocketFactory.getSocket(client.getIp());
            OutputStream os = socket.getOutputStream();
            os.write(Converter.convertToByteArray(message));
            os.flush();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
